package android.sdk;

import android.dto.DataTransferObject;
import android.dto.ScreenCoordinates;
import android.dto.TouchEvent;
import android.dto.ViewEvent;
import android.util.Log;
import android.view.MotionEvent;

public class TouchScreenController {
	
	private SDKClientService service;
	private NetworkController network;
	
	public TouchScreenController(SDKClientService service, NetworkController network){
		this.service = service;
		this.network = network;
	}

	public void registerView(int id, String name) {
		ViewEvent viewEvent = new ViewEvent();
		viewEvent.setType(DataTransferObject.TOUCHSCREEN_REGISTER_VIEW);
		viewEvent.setDeviceId(service.getDeviceId());
		viewEvent.setId(id);
		viewEvent.setName(name);
		network.sendObject(viewEvent);
	}

	public void onClick(int id) {
		ViewEvent viewEvent = new ViewEvent();
		viewEvent.setType(DataTransferObject.TOUCHSCREEN_CLICK_EVENT);
		viewEvent.setDeviceId(service.getDeviceId());
		viewEvent.setId(id);
		network.sendObject(viewEvent);
	}
	
	public void onLongClick(int id) {
		ViewEvent viewEvent = new ViewEvent();
		viewEvent.setType(DataTransferObject.TOUCHSCREEN_LONGCLICK_EVENT);
		viewEvent.setDeviceId(service.getDeviceId());
		viewEvent.setId(id);
		network.sendObject(viewEvent);
	}

	public void onTouch(int id, MotionEvent motionEvent) {
		TouchEvent touchEvent = new TouchEvent();
		touchEvent.setType(DataTransferObject.TOUCHSCREEN_TOUCH_EVENT);
		touchEvent.setDeviceId(service.getDeviceId());

		touchEvent.setId(id);
		Log.i("Tesis", "Enviando touch ID: " + touchEvent.getId());
		touchEvent.setAction(motionEvent.getAction());
		for (int pointer = 0; pointer < motionEvent.getPointerCount(); pointer++) {
			touchEvent.addCurrent(motionEvent.getPointerId(pointer), new ScreenCoordinates(motionEvent.getPointerId(pointer), motionEvent.getX(pointer), motionEvent.getY(pointer)));
			for (int pos = 0; pos < motionEvent.getHistorySize(); pos++)
				touchEvent.addHistorical(motionEvent.getPointerId(pointer), pos, new ScreenCoordinates(motionEvent.getPointerId(pointer), motionEvent.getHistoricalX(pointer, pos), motionEvent.getHistoricalY(pointer, pos)));
		}
		
		network.sendObject(touchEvent);
	}

}
